/*      */ package com.zimbra.cs.fb;
/*      */ 
/*      */ import com.microsoft.schemas.exchange.services._2006.messages.ArrayOfFreeBusyResponse;
/*      */ import com.microsoft.schemas.exchange.services._2006.messages.ArrayOfResponseMessagesType;
/*      */ import com.microsoft.schemas.exchange.services._2006.messages.CreateItemResponseType;
/*      */ import com.microsoft.schemas.exchange.services._2006.messages.CreateItemType;
/*      */ import com.microsoft.schemas.exchange.services._2006.messages.ExchangeService;
/*      */ import com.microsoft.schemas.exchange.services._2006.messages.ExchangeServicePortType;
/*      */ import com.microsoft.schemas.exchange.services._2006.messages.FindFolderResponseMessageType;
/*      */ import com.microsoft.schemas.exchange.services._2006.messages.FindFolderResponseType;
/*      */ import com.microsoft.schemas.exchange.services._2006.messages.FindFolderType;
/*      */ import com.microsoft.schemas.exchange.services._2006.messages.FindItemResponseMessageType;
/*      */ import com.microsoft.schemas.exchange.services._2006.messages.FindItemResponseType;
/*      */ import com.microsoft.schemas.exchange.services._2006.messages.FindItemType;
/*      */ import com.microsoft.schemas.exchange.services._2006.messages.FolderInfoResponseMessageType;
/*      */ import com.microsoft.schemas.exchange.services._2006.messages.FreeBusyResponseType;
/*      */ import com.microsoft.schemas.exchange.services._2006.messages.GetFolderResponseType;
/*      */ import com.microsoft.schemas.exchange.services._2006.messages.GetFolderType;
/*      */ import com.microsoft.schemas.exchange.services._2006.messages.GetUserAvailabilityRequestType;
/*      */ import com.microsoft.schemas.exchange.services._2006.messages.GetUserAvailabilityResponseType;
/*      */ import com.microsoft.schemas.exchange.services._2006.messages.ResponseMessageType;
/*      */ import com.microsoft.schemas.exchange.services._2006.messages.UpdateItemResponseType;
/*      */ import com.microsoft.schemas.exchange.services._2006.messages.UpdateItemType;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.ArrayOfFoldersType;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.ArrayOfMailboxData;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.ArrayOfRealItemsType;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.BaseFolderType;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.ConflictResolutionType;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.ConstantValueType;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.ContainmentModeType;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.ContainsExpressionType;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.DayOfWeekType;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.DefaultShapeNamesType;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.DistinguishedFolderIdNameType;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.DistinguishedFolderIdType;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.Duration;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.EmailAddress;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.ExchangeVersionType;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.ExtendedPropertyType;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.FieldURIOrConstantType;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.FindFolderParentType;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.FindItemParentType;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.FolderIdType;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.FolderQueryTraversalType;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.FolderResponseShapeType;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.FolderType;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.FreeBusyView;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.FreeBusyViewOptionsType;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.IsEqualToType;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.ItemChangeType;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.ItemQueryTraversalType;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.ItemResponseShapeType;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.ItemType;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.MailboxCultureType;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.MailboxData;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.MapiPropertyTypeType;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.MeetingAttendeeType;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.MessageDispositionType;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.NonEmptyArrayOfAllItemsType;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.NonEmptyArrayOfBaseFolderIdsType;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.NonEmptyArrayOfItemChangeDescriptionsType;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.NonEmptyArrayOfItemChangesType;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.NonEmptyArrayOfPropertyValuesType;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.PathToExtendedFieldType;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.PathToUnindexedFieldType;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.PostItemType;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.RequestServerVersion;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.ResponseClassType;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.RestrictionType;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.SearchExpressionType;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.SerializableTimeZone;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.SerializableTimeZoneTime;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.ServerVersionInfo;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.SetItemFieldType;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.TargetFolderIdType;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.TimeZoneContextType;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.TimeZoneDefinitionType;
/*      */ import com.microsoft.schemas.exchange.services._2006.types.UnindexedFieldURIType;
/*      */ import com.zimbra.common.account.Key.AccountBy;
/*      */ import com.zimbra.common.localconfig.KnownKey;
/*      */ import com.zimbra.common.localconfig.LC;
/*      */ import com.zimbra.common.service.ServiceException;
/*      */ import com.zimbra.common.util.Log;
/*      */ import com.zimbra.common.util.ZimbraLog;
/*      */ import com.zimbra.cs.account.Account;
/*      */ import com.zimbra.cs.account.Config;
/*      */ import com.zimbra.cs.account.Domain;
/*      */ import com.zimbra.cs.account.Provisioning;
/*      */ import com.zimbra.cs.mailbox.MailItem.Type;
/*      */ import java.io.IOException;
/*      */ import java.net.MalformedURLException;
/*      */ import java.net.URL;
/*      */ import java.security.cert.CertificateException;
/*      */ import java.security.cert.X509Certificate;
/*      */ import java.util.ArrayList;
/*      */ import java.util.Calendar;
/*      */ import java.util.EnumSet;
/*      */ import java.util.GregorianCalendar;
/*      */ import java.util.HashMap;
/*      */ import java.util.List;
/*      */ import java.util.Map;
/*      */ import java.util.Map.Entry;
/*      */ import java.util.Set;
/*      */ import java.util.TimeZone;
/*      */ import javax.activation.CommandMap;
/*      */ import javax.activation.MailcapCommandMap;
/*      */ import javax.net.ssl.HostnameVerifier;
/*      */ import javax.net.ssl.SSLSession;
/*      */ import javax.net.ssl.TrustManager;
/*      */ import javax.net.ssl.X509TrustManager;
/*      */ import javax.xml.bind.JAXBElement;
/*      */ import javax.xml.datatype.DatatypeConfigurationException;
/*      */ import javax.xml.datatype.DatatypeFactory;
/*      */ import javax.xml.namespace.QName;
/*      */ import javax.xml.ws.BindingProvider;
/*      */ import javax.xml.ws.Holder;
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ public class ExchangeEWSFreeBusyProvider
/*      */   extends FreeBusyProvider
/*      */ {
/*      */   public static final String TYPE_EWS = "ews";
/*  133 */   private ExchangeServicePortType service = null;
/*  134 */   private static ExchangeService factory = null;
/*      */   
/*      */ 
/*      */   private static TrustManager[] trustAllCerts;
/*      */   
/*      */ 
/*      */   private static HostnameVerifier hv;
/*      */   
/*      */ 
/*      */   private static ArrayList<ExchangeFreeBusyProvider.ExchangeUserResolver> sRESOLVERS;
/*      */   
/*      */ 
/*      */   private final HashMap<String, ArrayList<FreeBusyProvider.Request>> mRequests;
/*      */   
/*      */   private static final String EXCHANGE_EWS = "EXCHANGE2010";
/*      */   
/*      */ 
/*      */   boolean initService(ExchangeFreeBusyProvider.ServerInfo info)
/*      */     throws MalformedURLException
/*      */   {
/*  154 */     this.service = factory.getExchangeServicePort();
/*      */     
/*  156 */     ((BindingProvider)this.service).getRequestContext().put("javax.xml.ws.security.auth.username", info.authUsername);
/*      */     
/*  158 */     ((BindingProvider)this.service).getRequestContext().put("javax.xml.ws.security.auth.password", info.authPassword);
/*      */     
/*  160 */     ((BindingProvider)this.service).getRequestContext().put("javax.xml.ws.service.endpoint.address", info.url);
/*      */     
/*      */ 
/*      */ 
/*  164 */     return true;
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   private static class BasicUserResolver
/*      */     implements ExchangeFreeBusyProvider.ExchangeUserResolver
/*      */   {
/*      */     public ExchangeFreeBusyProvider.ServerInfo getServerInfo(String emailAddr)
/*      */     {
/*  210 */       String url = getAttr("zimbraFreebusyExchangeURL", emailAddr);
/*      */       
/*  212 */       String user = getAttr("zimbraFreebusyExchangeAuthUsername", emailAddr);
/*      */       
/*      */ 
/*  215 */       String pass = getAttr("zimbraFreebusyExchangeAuthPassword", emailAddr);
/*      */       
/*      */ 
/*  218 */       String scheme = getAttr("zimbraFreebusyExchangeAuthScheme", emailAddr);
/*      */       
/*      */ 
/*  221 */       if ((url == null) || (user == null) || (pass == null) || (scheme == null)) {
/*  222 */         return null;
/*      */       }
/*  224 */       ExchangeFreeBusyProvider.ServerInfo info = new ExchangeFreeBusyProvider.ServerInfo();
/*  225 */       info.url = url;
/*  226 */       info.authUsername = user;
/*  227 */       info.authPassword = pass;
/*  228 */       info.scheme = ExchangeFreeBusyProvider.AuthScheme.valueOf(scheme);
/*  229 */       info.org = getAttr("zimbraFreebusyExchangeUserOrg", emailAddr);
/*      */       try
/*      */       {
/*  232 */         Account acct = Provisioning.getInstance().get(Key.AccountBy.name, emailAddr);
/*      */         
/*  234 */         if (acct != null) {
/*  235 */           String[] fps = acct.getMultiAttr("zimbraForeignPrincipal");
/*      */           
/*  237 */           if ((fps != null) && (fps.length > 0)) {
/*  238 */             for (String fp : fps) {
/*  239 */               if (fp.startsWith("ad:")) {
/*  240 */                 int idx = fp.indexOf(':');
/*  241 */                 if (idx != -1) {
/*  242 */                   info.cn = fp.substring(idx + 1);
/*  243 */                   break;
/*      */                 }
/*      */               }
/*      */             }
/*      */           }
/*      */         }
/*      */       } catch (ServiceException se) {
/*  250 */         info.cn = null;
/*      */       }
/*  252 */       String exchangeType = getAttr("zimbraFreebusyExchangeServerType", emailAddr);
/*  253 */       info.enabled = "ews".equals(exchangeType);
/*  254 */       return info;
/*      */     }
/*      */     
/*      */     static String getAttr(String attr, String emailAddr)
/*      */     {
/*  259 */       String val = null;
/*  260 */       if (attr == null)
/*  261 */         return val;
/*      */       try {
/*  263 */         Provisioning prov = Provisioning.getInstance();
/*  264 */         if (emailAddr != null) {
/*  265 */           Account acct = prov.get(Key.AccountBy.name, emailAddr);
/*  266 */           if (acct != null) {
/*  267 */             val = acct.getAttr(attr, null);
/*  268 */             if (val != null)
/*  269 */               return val;
/*  270 */             Domain dom = prov.getDomain(acct);
/*  271 */             if (dom != null)
/*  272 */               val = dom.getAttr(attr, null);
/*  273 */             if (val != null)
/*  274 */               return val;
/*      */           }
/*      */         }
/*  277 */         val = prov.getConfig().getAttr(attr, null);
/*      */       } catch (ServiceException se) {
/*  279 */         ZimbraLog.fb.error("can't get attr " + attr, se);
/*      */       }
/*  281 */       return val;
/*      */     }
/*      */   }
/*      */   
/*      */   public ExchangeEWSFreeBusyProvider() {
/*  286 */     this.mRequests = new HashMap();
/*      */   }
/*      */   
/*      */   public boolean registerForMailboxChanges()
/*      */   {
/*  291 */     return registerForMailboxChanges(null);
/*      */   }
/*      */   
/*      */ 
/*      */   BaseFolderType bindFolder(DistinguishedFolderIdNameType distinguishedFolderId, DefaultShapeNamesType shapeType)
/*      */   {
/*  297 */     DistinguishedFolderIdType distinguishedFolderIdType = new DistinguishedFolderIdType();
/*      */     
/*  299 */     distinguishedFolderIdType.setId(distinguishedFolderId);
/*  300 */     NonEmptyArrayOfBaseFolderIdsType nonEmptyArrayOfBaseFolderIdsType = new NonEmptyArrayOfBaseFolderIdsType();
/*      */     
/*  302 */     nonEmptyArrayOfBaseFolderIdsType.getFolderIdOrDistinguishedFolderId().add(distinguishedFolderIdType);
/*      */     
/*  304 */     GetFolderType getFolderRequest = new GetFolderType();
/*  305 */     getFolderRequest.setFolderIds(nonEmptyArrayOfBaseFolderIdsType);
/*  306 */     FolderResponseShapeType stResp = new FolderResponseShapeType();
/*  307 */     stResp.setBaseShape(shapeType);
/*  308 */     getFolderRequest.setFolderShape(stResp);
/*  309 */     RequestServerVersion serverVersion = new RequestServerVersion();
/*  310 */     serverVersion.setVersion(ExchangeVersionType.EXCHANGE_2010_SP_1);
/*  311 */     Holder<ServerVersionInfo> gfversionInfo = new Holder();
/*      */     
/*  313 */     Holder<GetFolderResponseType> gfresponseHolder = new Holder();
/*      */     
/*  315 */     MailboxCultureType mct = new MailboxCultureType();
/*  316 */     mct.setValue("EN");
/*  317 */     TimeZoneDefinitionType tzdt = new TimeZoneDefinitionType();
/*  318 */     tzdt.setId("Greenwich Standard Time");
/*  319 */     TimeZoneContextType tzct = new TimeZoneContextType();
/*  320 */     tzct.setTimeZoneDefinition(tzdt);
/*  321 */     this.service.getFolder(getFolderRequest, mct, serverVersion, tzct, gfresponseHolder, gfversionInfo);
/*      */     
/*      */ 
/*      */ 
/*  325 */     FolderInfoResponseMessageType firmtResp = (FolderInfoResponseMessageType)((JAXBElement)((GetFolderResponseType)gfresponseHolder.value).getResponseMessages().getCreateItemResponseMessageOrDeleteItemResponseMessageOrGetItemResponseMessage().get(0)).getValue();
/*      */     
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*  331 */     if (firmtResp.getFolders().getFolderOrCalendarFolderOrContactsFolder().size() > 0)
/*      */     {
/*      */ 
/*  334 */       return (BaseFolderType)firmtResp.getFolders().getFolderOrCalendarFolderOrContactsFolder().get(0);
/*      */     }
/*      */     
/*      */ 
/*  338 */     ZimbraLog.fb.error("Could not find the folder in Exchange : " + distinguishedFolderId.toString());
/*      */     
/*      */ 
/*  341 */     return null;
/*      */   }
/*      */   
/*      */   List<BaseFolderType> findFolderByProp(FolderIdType id, UnindexedFieldURIType prop, String val)
/*      */   {
/*  346 */     FindFolderType findFolderRequest = new FindFolderType();
/*      */     
/*  348 */     findFolderRequest.setTraversal(FolderQueryTraversalType.SHALLOW);
/*  349 */     NonEmptyArrayOfBaseFolderIdsType ffEmptyArrayOfBaseFolderIdsType = new NonEmptyArrayOfBaseFolderIdsType();
/*      */     
/*  351 */     ffEmptyArrayOfBaseFolderIdsType.getFolderIdOrDistinguishedFolderId().add(id);
/*      */     
/*  353 */     FolderResponseShapeType stResp = new FolderResponseShapeType();
/*  354 */     stResp.setBaseShape(DefaultShapeNamesType.ID_ONLY);
/*  355 */     findFolderRequest.setParentFolderIds(ffEmptyArrayOfBaseFolderIdsType);
/*  356 */     findFolderRequest.setFolderShape(stResp);
/*      */     
/*  358 */     RestrictionType rtRestriction = new RestrictionType();
/*      */     
/*  360 */     IsEqualToType ieq = new IsEqualToType();
/*      */     
/*  362 */     PathToUnindexedFieldType pix = new PathToUnindexedFieldType();
/*  363 */     pix.setFieldURI(prop);
/*  364 */     ieq.setPath(new JAXBElement(new QName("http://schemas.microsoft.com/exchange/services/2006/types", "FieldURI"), PathToUnindexedFieldType.class, pix));
/*      */     
/*      */ 
/*      */ 
/*      */ 
/*  369 */     FieldURIOrConstantType ct = new FieldURIOrConstantType();
/*  370 */     ConstantValueType cv = new ConstantValueType();
/*  371 */     cv.setValue(val);
/*  372 */     ct.setConstant(cv);
/*      */     
/*  374 */     ieq.setFieldURIOrConstant(ct);
/*      */     
/*  376 */     rtRestriction.setSearchExpression(new JAXBElement(new QName("http://schemas.microsoft.com/exchange/services/2006/types", "IsEqualTo"), SearchExpressionType.class, ieq));
/*      */     
/*      */ 
/*      */ 
/*      */ 
/*  381 */     findFolderRequest.setRestriction(rtRestriction);
/*      */     
/*  383 */     Holder<FindFolderResponseType> findFolderResponse = new Holder();
/*      */     
/*  385 */     RequestServerVersion serverVersion = new RequestServerVersion();
/*  386 */     serverVersion.setVersion(ExchangeVersionType.EXCHANGE_2010_SP_1);
/*  387 */     Holder<ServerVersionInfo> gfversionInfo = new Holder();
/*      */     
/*      */ 
/*  390 */     MailboxCultureType mct = new MailboxCultureType();
/*  391 */     mct.setValue("EN");
/*  392 */     TimeZoneDefinitionType tzdt = new TimeZoneDefinitionType();
/*  393 */     tzdt.setId("Greenwich Standard Time");
/*  394 */     TimeZoneContextType tzct = new TimeZoneContextType();
/*  395 */     tzct.setTimeZoneDefinition(tzdt);
/*  396 */     this.service.findFolder(findFolderRequest, mct, serverVersion, tzct, findFolderResponse, gfversionInfo);
/*      */     
/*      */ 
/*      */ 
/*  400 */     FindFolderResponseMessageType ffRespMessage = (FindFolderResponseMessageType)((JAXBElement)((FindFolderResponseType)findFolderResponse.value).getResponseMessages().getCreateItemResponseMessageOrDeleteItemResponseMessageOrGetItemResponseMessage().get(0)).getValue();
/*      */     
/*      */ 
/*      */ 
/*      */ 
/*  405 */     if (ResponseClassType.SUCCESS == ffRespMessage.getResponseClass()) {
/*  406 */       return ffRespMessage.getRootFolder().getFolders().getFolderOrCalendarFolderOrContactsFolder();
/*      */     }
/*      */     
/*      */ 
/*  410 */     ZimbraLog.fb.warn("findFolderByProp " + ffRespMessage.getResponseCode());
/*  411 */     return null;
/*      */   }
/*      */   
/*      */   List<BaseFolderType> findFolderByPartialProp(FolderIdType id, UnindexedFieldURIType prop, String val)
/*      */   {
/*  416 */     FindFolderType findFolderRequest = new FindFolderType();
/*      */     
/*  418 */     findFolderRequest.setTraversal(FolderQueryTraversalType.SHALLOW);
/*  419 */     NonEmptyArrayOfBaseFolderIdsType ffEmptyArrayOfBaseFolderIdsType = new NonEmptyArrayOfBaseFolderIdsType();
/*      */     
/*  421 */     ffEmptyArrayOfBaseFolderIdsType.getFolderIdOrDistinguishedFolderId().add(id);
/*      */     
/*  423 */     FolderResponseShapeType stResp = new FolderResponseShapeType();
/*  424 */     stResp.setBaseShape(DefaultShapeNamesType.ID_ONLY);
/*  425 */     findFolderRequest.setParentFolderIds(ffEmptyArrayOfBaseFolderIdsType);
/*  426 */     findFolderRequest.setFolderShape(stResp);
/*      */     
/*  428 */     RestrictionType rtRestriction = new RestrictionType();
/*      */     
/*  430 */     ContainsExpressionType contains = new ContainsExpressionType();
/*  431 */     PathToUnindexedFieldType pix = new PathToUnindexedFieldType();
/*  432 */     pix.setFieldURI(prop);
/*  433 */     contains.setPath(new JAXBElement(new QName("http://schemas.microsoft.com/exchange/services/2006/types", "FieldURI"), PathToUnindexedFieldType.class, pix));
/*      */     
/*      */ 
/*      */ 
/*      */ 
/*  438 */     FieldURIOrConstantType ct = new FieldURIOrConstantType();
/*  439 */     ConstantValueType cv = new ConstantValueType();
/*  440 */     cv.setValue(val);
/*  441 */     ct.setConstant(cv);
/*      */     
/*  443 */     contains.setConstant(cv);
/*  444 */     contains.setContainmentMode(ContainmentModeType.SUBSTRING);
/*      */     
/*  446 */     rtRestriction.setSearchExpression(new JAXBElement(new QName("http://schemas.microsoft.com/exchange/services/2006/types", "Contains"), SearchExpressionType.class, contains));
/*      */     
/*      */ 
/*      */ 
/*      */ 
/*  451 */     findFolderRequest.setRestriction(rtRestriction);
/*      */     
/*  453 */     Holder<FindFolderResponseType> findFolderResponse = new Holder();
/*      */     
/*  455 */     RequestServerVersion serverVersion = new RequestServerVersion();
/*  456 */     serverVersion.setVersion(ExchangeVersionType.EXCHANGE_2010_SP_1);
/*  457 */     Holder<ServerVersionInfo> gfversionInfo = new Holder();
/*      */     
/*      */ 
/*  460 */     MailboxCultureType mct = new MailboxCultureType();
/*  461 */     mct.setValue("EN");
/*  462 */     TimeZoneDefinitionType tzdt = new TimeZoneDefinitionType();
/*  463 */     tzdt.setId("Greenwich Standard Time");
/*  464 */     TimeZoneContextType tzct = new TimeZoneContextType();
/*  465 */     tzct.setTimeZoneDefinition(tzdt);
/*  466 */     this.service.findFolder(findFolderRequest, mct, serverVersion, tzct, findFolderResponse, gfversionInfo);
/*      */     
/*      */ 
/*      */ 
/*  470 */     FindFolderResponseMessageType ffRespMessage = (FindFolderResponseMessageType)((JAXBElement)((FindFolderResponseType)findFolderResponse.value).getResponseMessages().getCreateItemResponseMessageOrDeleteItemResponseMessageOrGetItemResponseMessage().get(0)).getValue();
/*      */     
/*      */ 
/*      */ 
/*      */ 
/*  475 */     if (ResponseClassType.SUCCESS == ffRespMessage.getResponseClass()) {
/*  476 */       return ffRespMessage.getRootFolder().getFolders().getFolderOrCalendarFolderOrContactsFolder();
/*      */     }
/*      */     
/*      */ 
/*  480 */     ZimbraLog.fb.warn("findFolderByPartialProp " + ffRespMessage.getResponseCode());
/*      */     
/*  482 */     return null;
/*      */   }
/*      */   
/*      */   List<ItemType> findItemByProp(FolderIdType id, UnindexedFieldURIType prop, String val, DefaultShapeNamesType shapeType)
/*      */   {
/*  487 */     FindItemType findItemRequest = new FindItemType();
/*      */     
/*  489 */     RestrictionType rtRestriction = new RestrictionType();
/*      */     
/*  491 */     IsEqualToType ieq = new IsEqualToType();
/*      */     
/*  493 */     PathToUnindexedFieldType pix = new PathToUnindexedFieldType();
/*  494 */     pix.setFieldURI(prop);
/*  495 */     ieq.setPath(new JAXBElement(new QName("http://schemas.microsoft.com/exchange/services/2006/types", "FieldURI"), PathToUnindexedFieldType.class, pix));
/*      */     
/*      */ 
/*      */ 
/*      */ 
/*  500 */     FieldURIOrConstantType ct = new FieldURIOrConstantType();
/*  501 */     ConstantValueType cv = new ConstantValueType();
/*  502 */     cv.setValue(val);
/*  503 */     ct.setConstant(cv);
/*      */     
/*  505 */     ieq.setFieldURIOrConstant(ct);
/*      */     
/*  507 */     rtRestriction.setSearchExpression(new JAXBElement(new QName("http://schemas.microsoft.com/exchange/services/2006/types", "IsEqualTo"), SearchExpressionType.class, ieq));
/*      */     
/*      */ 
/*      */ 
/*      */ 
/*  512 */     findItemRequest.setRestriction(rtRestriction);
/*      */     
/*  514 */     ItemResponseShapeType stShape = new ItemResponseShapeType();
/*  515 */     stShape.setBaseShape(shapeType);
/*  516 */     findItemRequest.setItemShape(stShape);
/*  517 */     NonEmptyArrayOfBaseFolderIdsType ids = new NonEmptyArrayOfBaseFolderIdsType();
/*      */     
/*  519 */     ids.getFolderIdOrDistinguishedFolderId().add(id);
/*  520 */     findItemRequest.setParentFolderIds(ids);
/*  521 */     findItemRequest.setTraversal(ItemQueryTraversalType.SHALLOW);
/*      */     
/*  523 */     RequestServerVersion serverVersion = new RequestServerVersion();
/*  524 */     serverVersion.setVersion(ExchangeVersionType.EXCHANGE_2010_SP_1);
/*      */     
/*  526 */     Holder<FindItemResponseType> fiResponse = new Holder();
/*      */     
/*  528 */     Holder<ServerVersionInfo> gfversionInfo = new Holder();
/*      */     
/*  530 */     MailboxCultureType mct = new MailboxCultureType();
/*  531 */     mct.setValue("EN");
/*  532 */     TimeZoneDefinitionType tzdt = new TimeZoneDefinitionType();
/*  533 */     tzdt.setId("Greenwich Standard Time");
/*  534 */     TimeZoneContextType tzct = new TimeZoneContextType();
/*  535 */     tzct.setTimeZoneDefinition(tzdt);
/*  536 */     this.service.findItem(findItemRequest, mct, serverVersion, tzct, fiResponse, gfversionInfo);
/*      */     
/*      */ 
/*      */ 
/*      */ 
/*  541 */     FindItemResponseMessageType fiRespMessage = (FindItemResponseMessageType)((JAXBElement)((FindItemResponseType)fiResponse.value).getResponseMessages().getCreateItemResponseMessageOrDeleteItemResponseMessageOrGetItemResponseMessage().get(0)).getValue();
/*      */     
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*  547 */     if (ResponseClassType.SUCCESS == fiRespMessage.getResponseClass()) {
/*  548 */       return fiRespMessage.getRootFolder().getItems().getItemOrMessageOrCalendarItem();
/*      */     }
/*      */     
/*      */ 
/*  552 */     ZimbraLog.fb.warn("findItemByProp " + fiRespMessage.getResponseCode());
/*  553 */     return null;
/*      */   }
/*      */   
/*      */   public boolean handleMailboxChange(String accountId)
/*      */   {
/*  558 */     ZimbraLog.fb.debug("Entering handleMailboxChange() for account : " + accountId);
/*  559 */     String email = getEmailAddress(accountId);
/*  560 */     ExchangeFreeBusyProvider.ServerInfo serverInfo = getServerInfo(email);
/*  561 */     if ((email == null) || (!serverInfo.enabled)) {
/*  562 */       ZimbraLog.fb.debug("Exiting handleMailboxChange() for account : " + accountId);
/*  563 */       return true;
/*      */     }
/*      */     FreeBusy fb;
/*      */     try
/*      */     {
/*  568 */       fb = getFreeBusy(accountId, -1);
/*      */     } catch (ServiceException se) {
/*  570 */       ZimbraLog.fb.warn("can't get freebusy for account " + accountId, se);
/*  571 */       ZimbraLog.fb.debug("Exiting handleMailboxChange() for account : " + accountId);
/*      */       
/*  573 */       return !se.isReceiversFault();
/*      */     }
/*  575 */     if ((email == null) || (fb == null)) {
/*  576 */       ZimbraLog.fb.warn("account not found / incorrect / wrong host: " + accountId);
/*      */       
/*  578 */       ZimbraLog.fb.debug("Exiting handleMailboxChange() for account : " + accountId);
/*  579 */       return true;
/*      */     }
/*      */     
/*  582 */     if ((serverInfo == null) || (serverInfo.org == null) || (serverInfo.cn == null))
/*      */     {
/*  584 */       ZimbraLog.fb.warn("no exchange server info for user " + email);
/*  585 */       ZimbraLog.fb.debug("Exiting handleMailboxChange() for account : " + accountId);
/*  586 */       return true;
/*      */     }
/*  588 */     if (null == this.service) {
/*      */       try {
/*  590 */         if (!initService(serverInfo)) {
/*  591 */           ZimbraLog.fb.error("failed to initialize exchange service object " + serverInfo.url);
/*      */           
/*  593 */           ZimbraLog.fb.debug("Exiting handleMailboxChange() for account : " + accountId);
/*  594 */           return true;
/*      */         }
/*      */       } catch (MalformedURLException e) {
/*  597 */         ZimbraLog.fb.error("exception while trying to initialize exchange service object " + serverInfo.url);
/*      */         
/*  599 */         ZimbraLog.fb.debug("Exiting handleMailboxChange() for account : " + accountId);
/*  600 */         return true;
/*      */       }
/*      */     }
/*  603 */     ExchangeEWSMessage msg = new ExchangeEWSMessage(serverInfo.org, serverInfo.cn, email);
/*      */     
/*      */     try
/*      */     {
/*  607 */       FolderType publicFolderRoot = (FolderType)bindFolder(DistinguishedFolderIdNameType.PUBLICFOLDERSROOT, DefaultShapeNamesType.ALL_PROPERTIES);
/*      */       
/*      */ 
/*  610 */       if (publicFolderRoot == null) {
/*  611 */         ZimbraLog.fb.error("Could not find the public root folder on exchange");
/*  612 */         return true;
/*      */       }
/*  614 */       Object resultsNonIpm = findFolderByProp(publicFolderRoot.getParentFolderId(), UnindexedFieldURIType.FOLDER_DISPLAY_NAME, "NON_IPM_SUBTREE");
/*      */       
/*      */ 
/*      */       FolderType folderNonIPM;
/*      */       
/*  619 */       if ((resultsNonIpm != null) && (((List)resultsNonIpm).size() > 0)) {
/*  620 */         folderNonIPM = (FolderType)((List)resultsNonIpm).get(0);
/*      */         
/*  622 */         List<BaseFolderType> resultSchedulePlus = findFolderByProp(folderNonIPM.getFolderId(), UnindexedFieldURIType.FOLDER_DISPLAY_NAME, "SCHEDULE+ FREE BUSY");
/*      */         
/*      */ 
/*      */ 
/*  626 */         if ((resultSchedulePlus != null) && (resultSchedulePlus.size() > 0)) {
/*  627 */           FolderType folderSchedulePlus = (FolderType)resultSchedulePlus.get(0);
/*      */           
/*      */ 
/*  630 */           List<BaseFolderType> resultFBFolder = findFolderByPartialProp(folderSchedulePlus.getFolderId(), UnindexedFieldURIType.FOLDER_DISPLAY_NAME, serverInfo.org);
/*      */           
/*      */ 
/*      */           ResponseMessageType createItemResponseMessage;
/*      */           
/*  635 */           if ((resultFBFolder != null) && (resultFBFolder.size() > 0)) {
/*  636 */             FolderType folderFB = (FolderType)resultFBFolder.get(0);
/*      */             
/*  638 */             List<ItemType> resultMessage = findItemByProp(folderFB.getFolderId(), UnindexedFieldURIType.ITEM_SUBJECT, "USER-/CN=RECIPIENTS/CN=" + getForeignPrincipal(accountId), DefaultShapeNamesType.ALL_PROPERTIES);
/*      */             
/*      */ 
/*      */             ResponseMessageType updateItemResponseMessage;
/*      */             
/*      */ 
/*  644 */             if ((resultMessage != null) && (resultMessage.size() > 0))
/*      */             {
/*  646 */               ItemType itemMessage = (ItemType)resultMessage.get(0);
/*      */               
/*  648 */               Map<PathToExtendedFieldType, NonEmptyArrayOfPropertyValuesType> props = msg.GetFreeBusyProperties(fb);
/*      */               
/*      */ 
/*  651 */               NonEmptyArrayOfItemChangeDescriptionsType cdExPropArr = new NonEmptyArrayOfItemChangeDescriptionsType();
/*      */               
/*  653 */               for (PathToExtendedFieldType pathExProp : props.keySet()) {
/*  654 */                 ItemType itemEmptyMessage = new ItemType();
/*  655 */                 SetItemFieldType sifItem = new SetItemFieldType();
/*      */                 
/*  657 */                 sifItem.setPath(new JAXBElement(new QName("http://schemas.microsoft.com/exchange/services/2006/types", "Path"), PathToExtendedFieldType.class, pathExProp));
/*      */                 
/*      */ 
/*      */ 
/*  661 */                 ExtendedPropertyType exProp = new ExtendedPropertyType();
/*      */                 
/*  663 */                 exProp.setExtendedFieldURI(pathExProp);
/*  664 */                 if ((pathExProp.getPropertyType() == MapiPropertyTypeType.APPLICATION_TIME_ARRAY) || (pathExProp.getPropertyType() == MapiPropertyTypeType.BINARY_ARRAY) || (pathExProp.getPropertyType() == MapiPropertyTypeType.CLSID_ARRAY) || (pathExProp.getPropertyType() == MapiPropertyTypeType.CURRENCY_ARRAY) || (pathExProp.getPropertyType() == MapiPropertyTypeType.DOUBLE_ARRAY) || (pathExProp.getPropertyType() == MapiPropertyTypeType.FLOAT_ARRAY) || (pathExProp.getPropertyType() == MapiPropertyTypeType.INTEGER_ARRAY) || (pathExProp.getPropertyType() == MapiPropertyTypeType.LONG_ARRAY) || (pathExProp.getPropertyType() == MapiPropertyTypeType.OBJECT_ARRAY) || (pathExProp.getPropertyType() == MapiPropertyTypeType.SHORT_ARRAY) || (pathExProp.getPropertyType() == MapiPropertyTypeType.STRING_ARRAY) || (pathExProp.getPropertyType() == MapiPropertyTypeType.SYSTEM_TIME_ARRAY))
/*      */                 {
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*  676 */                   exProp.setValues((NonEmptyArrayOfPropertyValuesType)props.get(pathExProp));
/*      */                 }
/*  678 */                 else if (((NonEmptyArrayOfPropertyValuesType)props.get(pathExProp)).getValue().size() > 0) {
/*  679 */                   exProp.setValue((String)((NonEmptyArrayOfPropertyValuesType)props.get(pathExProp)).getValue().get(0));
/*      */                 }
/*      */                 
/*      */ 
/*      */ 
/*  684 */                 itemEmptyMessage.getExtendedProperty().add(exProp);
/*      */                 
/*  686 */                 sifItem.setItem(itemEmptyMessage);
/*  687 */                 cdExPropArr.getAppendToItemFieldOrSetItemFieldOrDeleteItemField().add(sifItem);
/*      */               }
/*      */               
/*      */ 
/*  691 */               UpdateItemType updateItemRequest = new UpdateItemType();
/*      */               
/*  693 */               updateItemRequest.setMessageDisposition(MessageDispositionType.SAVE_ONLY);
/*  694 */               updateItemRequest.setConflictResolution(ConflictResolutionType.ALWAYS_OVERWRITE);
/*  695 */               RequestServerVersion serverVersion = new RequestServerVersion();
/*      */               
/*  697 */               serverVersion.setVersion(ExchangeVersionType.EXCHANGE_2010_SP_1);
/*      */               
/*  699 */               ItemChangeType itemExpropChange = new ItemChangeType();
/*      */               
/*  701 */               itemExpropChange.setItemId(itemMessage.getItemId());
/*  702 */               itemExpropChange.setUpdates(cdExPropArr);
/*  703 */               NonEmptyArrayOfItemChangesType ctExPropArr = new NonEmptyArrayOfItemChangesType();
/*      */               
/*  705 */               ctExPropArr.getItemChange().add(itemExpropChange);
/*  706 */               updateItemRequest.setItemChanges(ctExPropArr);
/*      */               
/*  708 */               Holder<UpdateItemResponseType> updateItemResponse = new Holder();
/*      */               
/*  710 */               Holder<ServerVersionInfo> gfversionInfo = new Holder();
/*      */               
/*  712 */               MailboxCultureType mct = new MailboxCultureType();
/*  713 */               mct.setValue("EN");
/*  714 */               TimeZoneDefinitionType tzdt = new TimeZoneDefinitionType();
/*  715 */               tzdt.setId("Greenwich Standard Time");
/*  716 */               TimeZoneContextType tzct = new TimeZoneContextType();
/*  717 */               tzct.setTimeZoneDefinition(tzdt);
/*  718 */               this.service.updateItem(updateItemRequest, mct, serverVersion, tzct, updateItemResponse, gfversionInfo);
/*      */               
/*      */ 
/*      */ 
/*  722 */               updateItemResponseMessage = (ResponseMessageType)((JAXBElement)((UpdateItemResponseType)updateItemResponse.value).getResponseMessages().getCreateItemResponseMessageOrDeleteItemResponseMessageOrGetItemResponseMessage().get(0)).getValue();
/*      */ 
/*      */ 
/*      */             }
/*      */             else
/*      */             {
/*      */ 
/*      */ 
/*  730 */               PostItemType itemMessage = new PostItemType();
/*      */               
/*  732 */               itemMessage.setSubject("USER-/CN=RECIPIENTS/CN=" + getForeignPrincipal(accountId));
/*      */               
/*  734 */               itemMessage.setItemClass("IPM.Post");
/*      */               
/*  736 */               Map<PathToExtendedFieldType, NonEmptyArrayOfPropertyValuesType> props = msg.GetFreeBusyProperties(fb);
/*      */               
/*      */ 
/*  739 */               for (PathToExtendedFieldType pathExProp : props.keySet()) {
/*  740 */                 ExtendedPropertyType exProp = new ExtendedPropertyType();
/*      */                 
/*  742 */                 exProp.setExtendedFieldURI(pathExProp);
/*  743 */                 if ((pathExProp.getPropertyType() == MapiPropertyTypeType.APPLICATION_TIME_ARRAY) || (pathExProp.getPropertyType() == MapiPropertyTypeType.BINARY_ARRAY) || (pathExProp.getPropertyType() == MapiPropertyTypeType.CLSID_ARRAY) || (pathExProp.getPropertyType() == MapiPropertyTypeType.CURRENCY_ARRAY) || (pathExProp.getPropertyType() == MapiPropertyTypeType.DOUBLE_ARRAY) || (pathExProp.getPropertyType() == MapiPropertyTypeType.FLOAT_ARRAY) || (pathExProp.getPropertyType() == MapiPropertyTypeType.INTEGER_ARRAY) || (pathExProp.getPropertyType() == MapiPropertyTypeType.LONG_ARRAY) || (pathExProp.getPropertyType() == MapiPropertyTypeType.OBJECT_ARRAY) || (pathExProp.getPropertyType() == MapiPropertyTypeType.SHORT_ARRAY) || (pathExProp.getPropertyType() == MapiPropertyTypeType.STRING_ARRAY) || (pathExProp.getPropertyType() == MapiPropertyTypeType.SYSTEM_TIME_ARRAY))
/*      */                 {
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*  755 */                   exProp.setValues((NonEmptyArrayOfPropertyValuesType)props.get(pathExProp));
/*      */                 }
/*  757 */                 else if (((NonEmptyArrayOfPropertyValuesType)props.get(pathExProp)).getValue().size() > 0) {
/*  758 */                   exProp.setValue((String)((NonEmptyArrayOfPropertyValuesType)props.get(pathExProp)).getValue().get(0));
/*      */                 }
/*      */                 
/*      */ 
/*      */ 
/*  763 */                 itemMessage.getExtendedProperty().add(exProp);
/*      */               }
/*      */               
/*  766 */               CreateItemType createItemRequest = new CreateItemType();
/*      */               
/*  768 */               RequestServerVersion serverVersion = new RequestServerVersion();
/*      */               
/*  770 */               serverVersion.setVersion(ExchangeVersionType.EXCHANGE_2010_SP_1);
/*  771 */               createItemRequest.setMessageDisposition(MessageDispositionType.SAVE_ONLY);
/*  772 */               TargetFolderIdType idTargetFolder = new TargetFolderIdType();
/*      */               
/*  774 */               idTargetFolder.setFolderId(folderFB.getFolderId());
/*  775 */               createItemRequest.setSavedItemFolderId(idTargetFolder);
/*  776 */               NonEmptyArrayOfAllItemsType createItems = new NonEmptyArrayOfAllItemsType();
/*      */               
/*  778 */               createItems.getItemOrMessageOrCalendarItem().add(itemMessage);
/*      */               
/*  780 */               createItemRequest.setItems(createItems);
/*  781 */               Holder<CreateItemResponseType> createItemResponse = new Holder();
/*      */               
/*  783 */               Holder<ServerVersionInfo> gfversionInfo = new Holder();
/*      */               
/*  785 */               MailboxCultureType mct = new MailboxCultureType();
/*  786 */               mct.setValue("EN");
/*  787 */               TimeZoneDefinitionType tzdt = new TimeZoneDefinitionType();
/*  788 */               tzdt.setId("Greenwich Standard Time");
/*  789 */               TimeZoneContextType tzct = new TimeZoneContextType();
/*  790 */               tzct.setTimeZoneDefinition(tzdt);
/*  791 */               this.service.createItem(createItemRequest, mct, serverVersion, tzct, createItemResponse, gfversionInfo);
/*      */               
/*      */ 
/*      */ 
/*  795 */               createItemResponseMessage = (ResponseMessageType)((JAXBElement)((CreateItemResponseType)createItemResponse.value).getResponseMessages().getCreateItemResponseMessageOrDeleteItemResponseMessageOrGetItemResponseMessage().get(0)).getValue();
/*      */             }
/*      */             
/*      */ 
/*      */           }
/*      */           else
/*      */           {
/*      */ 
/*  803 */             ZimbraLog.fb.error("Could not find the Exchange folder containing '" + serverInfo.org + "'. Make sure zimbraFreebusyExchangeUserOrg is configured correctly and it exists on Exchange");
/*      */           }
/*      */         }
/*      */         else {
/*  807 */           ZimbraLog.fb.error("Could not find the Exchange folder 'SCHEDULE+ FREE BUSY'");
/*      */         }
/*      */       } else {
/*  810 */         ZimbraLog.fb.error("Could not find the Exchange folder 'NON_IPM_SUBTREE'");
/*      */       }
/*      */       
/*  813 */       return 1;
/*      */     }
/*      */     catch (Exception e) {
/*  816 */       ZimbraLog.fb.error("error communicating with " + serverInfo.url, e);
/*      */     } finally {
/*  818 */       ZimbraLog.fb.debug("Exiting handleMailboxChange() for account : " + accountId);
/*      */     }
/*      */     
/*  821 */     return false;
/*      */   }
/*      */   
/*      */   public List<FreeBusy> getFreeBusyForHost(String host, ArrayList<FreeBusyProvider.Request> req)
/*      */     throws IOException
/*      */   {
/*  827 */     int fb_interval = LC.exchange_free_busy_interval_min.intValueWithinRange(5, 1444);
/*  828 */     List<FreeBusyResponseType> results = null;
/*  829 */     ArrayList<FreeBusy> ret = new ArrayList();
/*      */     
/*  831 */     FreeBusyProvider.Request r = (FreeBusyProvider.Request)req.get(0);
/*  832 */     long start = FreeBusyProvider.Request.offsetInterval(((FreeBusyProvider.Request)req.get(0)).start, fb_interval);
/*  833 */     ExchangeFreeBusyProvider.ServerInfo serverInfo = (ExchangeFreeBusyProvider.ServerInfo)r.data;
/*  834 */     if (serverInfo == null) {
/*  835 */       ZimbraLog.fb.warn("no exchange server info for user " + r.email);
/*  836 */       return ret;
/*      */     }
/*      */     
/*  839 */     if (!serverInfo.enabled) {
/*  840 */       return ret;
/*      */     }
/*  842 */     ArrayOfMailboxData attendees = new ArrayOfMailboxData();
/*      */     
/*  844 */     for (FreeBusyProvider.Request request : req) {
/*  845 */       EmailAddress email = new EmailAddress();
/*  846 */       email.setAddress(request.email);
/*  847 */       MailboxData mailbox = new MailboxData();
/*  848 */       mailbox.setEmail(email);
/*  849 */       mailbox.setAttendeeType(MeetingAttendeeType.REQUIRED);
/*  850 */       attendees.getMailboxData().add(mailbox);
/*      */     }
/*      */     try {
/*  853 */       Duration duration = new Duration();
/*  854 */       DatatypeFactory datatypeFactory = DatatypeFactory.newInstance();
/*      */       
/*  856 */       GregorianCalendar gregorianCalStart = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
/*  857 */       gregorianCalStart.setTimeInMillis(start);
/*  858 */       duration.setStartTime(datatypeFactory.newXMLGregorianCalendar(gregorianCalStart));
/*      */       
/*  860 */       GregorianCalendar gregorianCalEnd = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
/*  861 */       gregorianCalEnd.setTimeInMillis(((FreeBusyProvider.Request)req.get(0)).end);
/*  862 */       duration.setEndTime(datatypeFactory.newXMLGregorianCalendar(gregorianCalEnd));
/*      */       
/*  864 */       FreeBusyViewOptionsType availabilityOpts = new FreeBusyViewOptionsType();
/*      */       
/*  866 */       availabilityOpts.setMergedFreeBusyIntervalInMinutes(Integer.valueOf(fb_interval));
/*      */       
/*  868 */       availabilityOpts.getRequestedView().add("MergedOnly");
/*  869 */       availabilityOpts.setTimeWindow(duration);
/*  870 */       GetUserAvailabilityRequestType availabilityRequest = new GetUserAvailabilityRequestType();
/*      */       
/*      */ 
/*  873 */       SerializableTimeZone timezone = new SerializableTimeZone();
/*  874 */       timezone.setBias(0);
/*  875 */       SerializableTimeZoneTime standardTime = new SerializableTimeZoneTime();
/*      */       
/*  877 */       standardTime.setTime("00:00:00");
/*  878 */       standardTime.setDayOrder((short)1);
/*  879 */       standardTime.setDayOfWeek(DayOfWeekType.SUNDAY);
/*  880 */       timezone.setStandardTime(standardTime);
/*  881 */       timezone.setDaylightTime(standardTime);
/*  882 */       availabilityRequest.setTimeZone(timezone);
/*      */       
/*  884 */       availabilityRequest.setFreeBusyViewOptions(availabilityOpts);
/*  885 */       availabilityRequest.setMailboxDataArray(attendees);
/*  886 */       RequestServerVersion serverVersion = new RequestServerVersion();
/*  887 */       serverVersion.setVersion(ExchangeVersionType.EXCHANGE_2010_SP_1);
/*  888 */       Holder<GetUserAvailabilityResponseType> availabilityResponse = new Holder();
/*      */       
/*  890 */       Holder<ServerVersionInfo> gfversionInfo = new Holder();
/*      */       
/*      */ 
/*  893 */       this.service.getUserAvailability(availabilityRequest, availabilityResponse, gfversionInfo);
/*      */       
/*      */ 
/*  896 */       results = ((GetUserAvailabilityResponseType)availabilityResponse.value).getFreeBusyResponseArray().getFreeBusyResponse();
/*      */     }
/*      */     catch (DatatypeConfigurationException dce)
/*      */     {
/*  900 */       ZimbraLog.fb.warn("getFreeBusyForHost DatatypeConfiguration failure", dce);
/*      */       
/*  902 */       return getEmptyList(req);
/*      */     } catch (Exception e) {
/*  904 */       ZimbraLog.fb.warn("getFreeBusyForHost failure", e);
/*  905 */       return getEmptyList(req);
/*      */     }
/*      */     
/*  908 */     for (FreeBusyProvider.Request re : req) {
/*  909 */       String fb = "";
/*  910 */       int i = 0;
/*  911 */       for (FreeBusyResponseType attendeeAvailability : results) {
/*  912 */         if (re.email == ((MailboxData)attendees.getMailboxData().get(i)).getEmail().getAddress())
/*      */         {
/*  914 */           if (ResponseClassType.SUCCESS != attendeeAvailability.getResponseMessage().getResponseClass())
/*      */           {
/*  916 */             ZimbraLog.fb.warn(attendeeAvailability.getResponseMessage().getMessageText());
/*      */             
/*  918 */             ZimbraLog.fb.warn("Error in response. continuing to next one");
/*      */             
/*      */ 
/*  921 */             i++;
/*      */           }
/*      */           else {
/*  924 */             ZimbraLog.fb.debug("Availability for " + ((MailboxData)attendees.getMailboxData().get(i)).getEmail().getAddress() + " [" + attendeeAvailability.getFreeBusyView().getMergedFreeBusy() + "]");
/*      */             
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*  931 */             fb = attendeeAvailability.getFreeBusyView().getMergedFreeBusy();
/*  932 */             if (fb != null) break;
/*  933 */             ZimbraLog.fb.warn("Merged view Free Busy info not avaiable");
/*  934 */             fb = ""; break;
/*      */           }
/*      */           
/*      */         }
/*      */         else {
/*  939 */           i++;
/*      */         }
/*      */       }
/*  942 */       ret.add(new ExchangeFreeBusyProvider.ExchangeUserFreeBusy(fb, re.email, fb_interval, ((FreeBusyProvider.Request)req.get(0)).start, ((FreeBusyProvider.Request)req.get(0)).end));
/*      */     }
/*      */     
/*      */ 
/*  946 */     return ret;
/*      */   }
/*      */   
/*      */   public static int checkAuth(ExchangeFreeBusyProvider.ServerInfo info, Account requestor) throws ServiceException, IOException
/*      */   {
/*  951 */     ExchangeEWSFreeBusyProvider provider = new ExchangeEWSFreeBusyProvider();
/*  952 */     provider.initService(info);
/*  953 */     FolderType publicFolderRoot = (FolderType)provider.bindFolder(DistinguishedFolderIdNameType.PUBLICFOLDERSROOT, DefaultShapeNamesType.ALL_PROPERTIES);
/*      */     
/*      */ 
/*  956 */     if (publicFolderRoot == null) {
/*  957 */       return 400;
/*      */     }
/*  959 */     return 200;
/*      */   }
/*      */   
/*      */   public ExchangeEWSFreeBusyProvider getInstance()
/*      */   {
/*  964 */     return new ExchangeEWSFreeBusyProvider();
/*      */   }
/*      */   
/*      */   public void addFreeBusyRequest(FreeBusyProvider.Request req) throws FreeBusyProvider.FreeBusyUserNotFoundException
/*      */   {
/*  969 */     ExchangeFreeBusyProvider.ServerInfo info = null;
/*  970 */     for (ExchangeFreeBusyProvider.ExchangeUserResolver resolver : sRESOLVERS) {
/*  971 */       String email = req.email;
/*  972 */       if (req.requestor != null)
/*  973 */         email = req.requestor.getName();
/*  974 */       info = resolver.getServerInfo(email);
/*  975 */       if (info != null) {
/*  976 */         if (!info.enabled)
/*  977 */           throw new FreeBusyProvider.FreeBusyUserNotFoundException();
/*  978 */         if (null != this.service)
/*      */           break;
/*  980 */         try { initService(info);
/*      */         } catch (MalformedURLException e) {
/*  982 */           ZimbraLog.fb.warn("failed to initialize provider", e);
/*      */         }
/*      */       }
/*      */     }
/*      */     
/*      */ 
/*  988 */     if (info == null)
/*  989 */       throw new FreeBusyProvider.FreeBusyUserNotFoundException();
/*  990 */     addRequest(info, req);
/*      */   }
/*      */   
/*      */   public static void registerResolver(ExchangeFreeBusyProvider.ExchangeUserResolver r, int priority) {
/*  994 */     synchronized (sRESOLVERS) {
/*  995 */       sRESOLVERS.ensureCapacity(priority + 1);
/*  996 */       sRESOLVERS.add(priority, r);
/*      */     }
/*      */   }
/*      */   
/*      */   static
/*      */   {
/*  137 */     ZimbraLog.fb.debug("Setting MailcapCommandMap handlers back to default");
/*  138 */     MailcapCommandMap mc = (MailcapCommandMap)CommandMap.getDefaultCommandMap();
/*  139 */     mc.addMailcap("application/xml;;x-java-content-handler=com.sun.mail.handlers.text_xml");
/*  140 */     mc.addMailcap("text/xml;;x-java-content-handler=com.sun.mail.handlers.text_xml");
/*  141 */     mc.addMailcap("text/plain;;x-java-content-handler=com.sun.mail.handlers.text_plain");
/*  142 */     mc.addMailcap("xml/x-zimbra-share;;x-java-content-handler=com.sun.mail.handlers.text_plain");
/*  143 */     CommandMap.setDefaultCommandMap(mc);
/*  144 */     ZimbraLog.fb.debug("Done Setting MailcapCommandMap handlers");
/*      */     
/*  146 */     URL wsdlUrl = ExchangeService.class.getResource("/EWS.wsdl");
/*  147 */     ZimbraLog.fb.debug("EWS Wsdl URL = %s", new Object[] { wsdlUrl });
/*  148 */     factory = new ExchangeService(wsdlUrl, new QName("http://schemas.microsoft.com/exchange/services/2006/messages", "ExchangeService"));
/*      */     
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*  167 */     trustAllCerts = new TrustManager[] { new X509TrustManager()
/*      */     {
/*      */ 
/*      */       public X509Certificate[] getAcceptedIssuers()
/*      */       {
/*  172 */         return null;
/*      */       }
/*      */       
/*      */ 
/*      */ 
/*      */ 
/*      */       public void checkServerTrusted(X509Certificate[] chain, String authType)
/*      */         throws CertificateException
/*      */       {}
/*      */       
/*      */ 
/*      */ 
/*      */ 
/*      */       public void checkClientTrusted(X509Certificate[] chain, String authType)
/*      */         throws CertificateException
/*      */       {}
/*  188 */     } };
/*  189 */     hv = new HostnameVerifier()
/*      */     {
/*      */ 
/*      */       public boolean verify(String hostname, SSLSession session)
/*      */       {
/*  194 */         return true;
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */       }
/*      */       
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/* 1001 */     };
/* 1002 */     sRESOLVERS = new ArrayList();
/*      */     
/* 1004 */     registerResolver(new BasicUserResolver(null), 0);
/* 1005 */     register(new ExchangeEWSFreeBusyProvider());
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */   public List<FreeBusy> getResults()
/*      */   {
/* 1012 */     ArrayList<FreeBusy> ret = new ArrayList();
/* 1013 */     for (Map.Entry<String, ArrayList<FreeBusyProvider.Request>> entry : this.mRequests.entrySet()) {
/*      */       try {
/* 1015 */         ret.addAll(getFreeBusyForHost((String)entry.getKey(), (ArrayList)entry.getValue()));
/*      */       }
/*      */       catch (IOException e) {
/* 1018 */         ZimbraLog.fb.error("error communicating with " + (String)entry.getKey(), e);
/*      */       }
/*      */     }
/*      */     
/* 1022 */     return ret;
/*      */   }
/*      */   
/*      */   protected void addRequest(ExchangeFreeBusyProvider.ServerInfo info, FreeBusyProvider.Request req) {
/* 1026 */     ArrayList<FreeBusyProvider.Request> r = (ArrayList)this.mRequests.get(info.url);
/* 1027 */     if (r == null) {
/* 1028 */       r = new ArrayList();
/* 1029 */       this.mRequests.put(info.url, r);
/*      */     }
/* 1031 */     req.data = info;
/* 1032 */     r.add(req);
/*      */   }
/*      */   
/*      */   public ExchangeFreeBusyProvider.ServerInfo getServerInfo(String emailAddr) {
/* 1036 */     ExchangeFreeBusyProvider.ServerInfo serverInfo = null;
/* 1037 */     for (ExchangeFreeBusyProvider.ExchangeUserResolver r : sRESOLVERS) {
/* 1038 */       serverInfo = r.getServerInfo(emailAddr);
/* 1039 */       if (serverInfo != null)
/*      */         break;
/*      */     }
/* 1042 */     return serverInfo;
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */   public String getName()
/*      */   {
/* 1049 */     return "EXCHANGE2010";
/*      */   }
/*      */   
/*      */   public Set<MailItem.Type> registerForItemTypes()
/*      */   {
/* 1054 */     return EnumSet.of(MailItem.Type.APPOINTMENT);
/*      */   }
/*      */   
/*      */   private long getTimeInterval(String attr, String accountId, long defaultValue) throws ServiceException {
/* 1058 */     Provisioning prov = Provisioning.getInstance();
/* 1059 */     if (accountId != null) {
/* 1060 */       Account acct = prov.get(Key.AccountBy.id, accountId);
/* 1061 */       if (acct != null) {
/* 1062 */         return acct.getTimeInterval(attr, defaultValue);
/*      */       }
/*      */     }
/* 1065 */     return prov.getConfig().getTimeInterval(attr, defaultValue);
/*      */   }
/*      */   
/*      */   public long cachedFreeBusyStartTime(String accountId)
/*      */   {
/* 1070 */     Calendar cal = GregorianCalendar.getInstance();
/* 1071 */     int curYear = cal.get(1);
/*      */     try {
/* 1073 */       long dur = getTimeInterval("zimbraFreebusyExchangeCachedIntervalStart", accountId, 0L);
/* 1074 */       cal.setTimeInMillis(System.currentTimeMillis() - dur);
/*      */     }
/*      */     catch (ServiceException se) {
/* 1077 */       cal.setTimeInMillis(System.currentTimeMillis() - 604800000L);
/*      */     }
/*      */     
/*      */ 
/* 1081 */     cal.set(11, 0);
/* 1082 */     cal.set(12, 0);
/* 1083 */     cal.set(13, 0);
/* 1084 */     if (cal.get(1) < curYear)
/*      */     {
/*      */ 
/* 1087 */       cal.set(curYear, 0, 1);
/*      */     }
/* 1089 */     return cal.getTimeInMillis();
/*      */   }
/*      */   
/*      */   public long cachedFreeBusyEndTime(String accountId)
/*      */   {
/* 1094 */     long duration = 5356800000L;
/* 1095 */     Calendar cal = GregorianCalendar.getInstance();
/*      */     try {
/* 1097 */       duration = getTimeInterval("zimbraFreebusyExchangeCachedInterval", accountId, duration);
/*      */     } catch (ServiceException se) {}
/* 1099 */     cal.setTimeInMillis(cachedFreeBusyStartTime(accountId) + duration);
/*      */     
/* 1101 */     cal.set(11, 0);
/* 1102 */     cal.set(12, 0);
/* 1103 */     cal.set(13, 0);
/* 1104 */     return cal.getTimeInMillis();
/*      */   }
/*      */   
/*      */   public long cachedFreeBusyStartTime()
/*      */   {
/* 1109 */     return cachedFreeBusyStartTime(null);
/*      */   }
/*      */   
/*      */   public long cachedFreeBusyEndTime()
/*      */   {
/* 1114 */     return cachedFreeBusyEndTime(null);
/*      */   }
/*      */   
/*      */   public String foreignPrincipalPrefix()
/*      */   {
/* 1119 */     return "ad:";
/*      */   }
/*      */   
/*      */   protected String getForeignPrincipal(String accountId) throws ServiceException {
/* 1123 */     String ret = null;
/* 1124 */     Account acct = Provisioning.getInstance().get(Key.AccountBy.id, accountId);
/*      */     
/*      */ 
/* 1127 */     if (acct == null)
/* 1128 */       return null;
/* 1129 */     String[] fps = acct.getForeignPrincipal();
/* 1130 */     for (String fp : fps) {
/* 1131 */       if (fp.startsWith("ad:")) {
/* 1132 */         int idx = fp.indexOf(':');
/* 1133 */         if (idx != -1) {
/* 1134 */           ret = fp.substring(idx + 1);
/* 1135 */           break;
/*      */         }
/*      */       }
/*      */     }
/* 1139 */     return ret;
/*      */   }
/*      */   
/*      */   public boolean registerForMailboxChanges(String accountId)
/*      */   {
/* 1144 */     if (sRESOLVERS.size() > 1)
/* 1145 */       return true;
/* 1146 */     String email = null;
/*      */     try {
/* 1148 */       Account account = null;
/* 1149 */       if (accountId != null)
/* 1150 */         account = Provisioning.getInstance().getAccountById(accountId);
/* 1151 */       if (account != null)
/* 1152 */         email = account.getName();
/*      */     } catch (ServiceException se) {
/* 1154 */       ZimbraLog.fb.warn("cannot fetch account", se);
/*      */     }
/* 1156 */     return getServerInfo(email) != null;
/*      */   }
/*      */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/fb/ExchangeEWSFreeBusyProvider.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */